#!/usr/bin/env python
# coding: utf-8

# In[1]:


import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow.keras import layers
import joblib
import pymysql

gpus = tf.config.experimental.list_physical_devices(device_type='GPU')
for gpu in gpus:
    tf.config.experimental.set_memory_growth(gpu, True)

# In[2]:


conn = pymysql.connect(host='127.0.0.1', user='root', password='', charset='utf8mb4', port=3306, database='demo')

# In[3]:


df = pd.read_sql('select * from train', conn)
df['date'] = pd.to_datetime(df['date'])

conn.close()

# In[4]:


# print(df)

# In[5]:


window_size = 24

# In[6]:


# # 定义模型
# model = tf.keras.Sequential([
#     layers.Input((window_size, 1)),
#     layers.Bidirectional(layers.LSTM(64)),
#
#     layers.Dense(32, activation='relu'),
#     layers.Dense(1)
# ])
#
# model.compile(loss="mse", optimizer='adam', metrics=['mae'])
# model.load_weights('model-气温.h5')
# model.summary()

# In[7]:


df['temperature'] = df['temperature'].astype(np.float64)


def to_pre(times, model):
    """
    num: 预测后面的num小时
    time: 开始时间
    """
    temp = []
    start_index = df['date'][df['date'] == pd.to_datetime('{} 00:00:00'.format(times))].index[0]
    b = df.loc[start_index - window_size: start_index - 1, 'temperature'].values

    for i in range(24):
        begin = b[-window_size:].reshape(1, window_size, 1)
        pre = model.predict(begin)[0]
        temp.append(pre[0])
        b = np.insert(b, -1, pre)
    return temp

# In[10]:


# print(to_pre('2017-01-01'))

# In[ ]:
